package com.ckjw.gobang.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Event;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

import com.ckjw.gobang.utils.MyIPTool;

//import com.bzchao.gobang.view.MainFrame;

/**
 * 界面菜单栏类
 * 
 * @author lck and djw
 * @Date 2022年6月3日-下午10:34:33
 */
public class MyMenuBar extends JMenuBar {
	// TODO
	private static final long serialVersionUID = 1L;
	// 定义菜单选项
	JMenuItem checkIP, setInfo, exit, about, version, setTime, setLevel,   admin, word, gameOnline;
	static JMenuItem login;
	static JMenu my ;
	public static MyMenuBar myMenuBar;
	
	//定义用户姓名标签
	public static JMenu wel;
	// 定义构造器
	public MyMenuBar() {
		UIManager.put("Menu.font", new Font("宋体", Font.BOLD, 22));
		UIManager.put("MenuItem.font", new Font("宋体", Font.BOLD, 22));
		
		wel=new JMenu();
		wel.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 22));
		wel.setForeground(Color.RED);
		
		// 定义菜单条目
		JMenu menu = new JMenu("菜单(F)");
		// TODO
		menu.setMnemonic('f'); // 助记符

		my = new JMenu("登录(M)");
		my.setMnemonic('m'); // 助记符

		JMenu help = new JMenu("帮助(H)");
		help.setMnemonic('h'); // 助记符

		// 定义菜单下的选项按钮
		login = new JMenuItem("登录/注册(d)");
		login.setMnemonic('d');
		checkIP = new JMenuItem("查看本机IP");
		exit = new JMenuItem("退出");
		about = new JMenuItem("开发说明");
		version = new JMenuItem("软件版本");
		word = new JMenuItem("游戏说明");
		gameOnline = new JMenuItem("在线打地鼠");
		
		menu.add(checkIP);
		menu.add(exit);
		my.add(login);
		//my.add(register);
		help.add(about);
		help.add(version);
		help.add(word);
		help.add(gameOnline);

		this.add(menu);
		this.add(my);
		this.add(help);
		this.add(Box.createHorizontalStrut(120));
		this.add(wel);
		addListener();
		myMenuBar=this;
	}

	/**
	 * 为菜单选项添加监听器
	 */
	private void addListener() {
		//菜单下的按钮监听器
		exit.addActionListener(event -> MainInterface.close());
		 checkIP.addActionListener(event -> {
	            String localIP = "本机所有IP地址:";
	            List<String> res = MyIPTool.getAllLocalHostIP();
	            String allIp = res.stream().collect(Collectors.joining("\n"));
	            localIP = localIP + "\n" + allIp;

	            JOptionPane.showMessageDialog(null, localIP, "查看本机IP", JOptionPane.INFORMATION_MESSAGE);
	        });
		 //登录的监听器
		 login.addActionListener(event ->{
			 //实现登陆功能
			try {
				new login().init();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 	});
		 
		 
		 //帮助栏
		 about.addActionListener(event -> {
            String function = "在线五子棋";
            String person = "\n软件编码：李传开 1529077777\n软件测试：戴俊伟 13142435525";
            String info = function + person + "\n开发时间：" + MainInterface.BUILD_TIME + "\n"
                    + MainInterface.COPYRIGHT;
            JOptionPane.showMessageDialog(null, info, "软件开发说明",
                    JOptionPane.INFORMATION_MESSAGE);
        });
		 version.addActionListener(event -> {
            String info = " 软件版本：" + MainInterface.VERSION + "\n 开发日期 " + MainInterface.BUILD_TIME + "\n" + MainInterface.COPYRIGHT;
            JOptionPane.showMessageDialog(null, info, "软件开发说明",
                    JOptionPane.INFORMATION_MESSAGE);
        });
		 word.addActionListener(event -> JOptionPane
	                .showMessageDialog(
	                        MainInterface.mainInterface,
	                        "游戏名称：五子棋小游戏\n主要功能：本地对战，网络对战\n提供登陆注册账号服务",
	                        "游戏说明", JOptionPane.INFORMATION_MESSAGE));
		 gameOnline.addActionListener(event -> {
            try {
                java.net.URI uri = new java.net.URI("http://lck.lichuankai.top/weeks13/examp/mouse_game.html");
                Desktop.getDesktop().browse(uri);
            } catch (Exception e) {
            }
        });
		
	}
}
